package com.etc.delivery.controller.warehouse;

import com.etc.delivery.entity.Driver;
import com.etc.delivery.entity.MailType;
import com.etc.delivery.entity.Warehouse;
import com.etc.delivery.entity.Waybill;
import com.etc.delivery.service.warehouse.IDriverService;
import com.etc.delivery.service.warehouse.IWarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@SessionAttributes(value = {"waybill", "waybill1"})
@Controller
@RequestMapping("/warehouse")
public class WarechouseController {

    @Resource
    private IWarehouseService warehouseService;

    @Resource(name = "driverServiceImpl2")
    private IDriverService iDriverService;


    @RequestMapping("/login")
    public ModelAndView login(String phone, String password, HttpServletRequest request) {

        HttpSession session = request.getSession();
        Warehouse warehouse = new Warehouse();
        warehouse.setPhone(phone);
        warehouse.setPassword(password);
        List<Warehouse> list = warehouseService.login(warehouse);

        for (Warehouse w : list
        ) {
            System.out.println(w);
        }
        ModelAndView mv = new ModelAndView();

        if (list == null || list.isEmpty()) {
            mv.setViewName("warehouse/login");//失败
        } else {

            session.setAttribute("warehouse_id", list.get(0).getId());

            mv.setViewName("warehouse/Index");//成功
        }
        return mv;
    }

    @RequestMapping("/queryAllByAjax")
    @ResponseBody
    public List<Waybill> queryAllByAjax(String waybillNo
            , String mailType, String senderName,
                                        String senderAddress, String recipientName,
                                        String recipientAddress,
                                        String mailCost, String status, HttpServletRequest request
    ) {
        HttpSession session = request.getSession();
        Integer warehouse_id = (Integer) session.getAttribute("warehouse_id");
        int id = 0;
        int status1 = 0;
        if (!waybillNo.isEmpty()) {
            id = Integer.parseInt(waybillNo);
        }
//        System.out.println(id);
        if (!status.isEmpty()) {
            status1 = Integer.parseInt(status);

        }
        String sender_phone = "";
        String recipient_phone = "";

        List<Waybill> list = warehouseService.queryAllByAjax(id
                , mailType, senderName, sender_phone, senderAddress, recipientName, recipient_phone, recipientAddress, mailCost, status1,
                warehouse_id
        );
        for (Waybill g : list) {
            System.out.println(g);
        }
        return list;
    }


    @RequestMapping("/addWarehouse")
    public String addWarehouse(int waybillNo) {
        boolean flig = warehouseService.addWaybil(waybillNo);

        return "warehouse/apply";
    }

    @ResponseBody
    @RequestMapping("/deleteWaybill")
    public String deleteWaybill(int waybill_no) {
        System.out.println(waybill_no);
        warehouseService.deleteWaybill(waybill_no);
        return "list";
    }

    @ResponseBody
    @RequestMapping("/updateByStatus")
    public String updateByStatus(int waybill_no) {
        warehouseService.updateByStatus(waybill_no);
        return "list";
    }

    @ResponseBody
    @RequestMapping("/queryAllDriver")
    public List<Driver> queryAllDriver(Integer warehouse_id, HttpServletRequest request, String name) {

        HttpSession session = request.getSession();
        warehouse_id = (Integer) session.getAttribute("warehouse_id");
//        System.out.println(warehouse_id);
        System.out.println("name" + name);
        List<Driver> list = iDriverService.queryAllDriverByWarehouse_id(warehouse_id, name);
        for (Driver d : list
        ) {
            System.out.println(d);
        }
        return list;
    }

    @RequestMapping("/updateWaybill")
    public String updateWaybill(String mail_type, String sender_name, String sender_phone,
                                String sender_address, String recipient_name,
                                String recipient_phone, String recipient_address, String mail_cost, String waybill_no) {

        System.out.println("测试");
        System.out.println(mail_type);

        int mail_cost1 = 0;
        int waybill_no1 = 0;
        int mail_type1 = 0;
        if (!mail_cost.isEmpty()) {
            mail_cost1 = Integer.parseInt(mail_cost);
        }

        if (!waybill_no.isEmpty()) {
            waybill_no1 = Integer.parseInt(waybill_no);
        }
        if (!mail_type.isEmpty()) {
            mail_type1 = Integer.parseInt(mail_type);
        }
        boolean flog = warehouseService.updateWaybill(mail_type1, sender_name, sender_phone, sender_address, recipient_name, recipient_phone
                , recipient_address, mail_cost1, waybill_no1);
        System.out.println(flog);
        return "warehouse/list";
    }


    @RequestMapping("/queryWaybill")
    public ModelAndView queryWaybill(int waybill_no, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer warehouse_id = (Integer) session.getAttribute("warehouse_id");
        List<Waybill> waybill = warehouseService.queryAllBywaybill_no(waybill_no, warehouse_id);

        session.setAttribute("waybill_no1", waybill_no);
        System.out.println(waybill_no);
        ModelAndView mv = new ModelAndView();
        mv.addObject("waybill", waybill);
        mv.setViewName("warehouse/Replace_list");
        return mv;
    }

    @RequestMapping("/queryWaybill1")
    public ModelAndView queryWaybill1(int waybill_no, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer warehouse_id = (Integer) session.getAttribute("warehouse_id");
        List<Waybill> waybill = warehouseService.queryAllBywaybill_no(waybill_no, warehouse_id);
        session.setAttribute("waybill_no1", waybill_no);
        System.out.println(waybill_no);
        ModelAndView mv = new ModelAndView();
        mv.addObject("waybill1", waybill);
        mv.setViewName("warehouse/apply");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/deleteDriver")
    public String deleteDriver(int id) {
        iDriverService.deleteDriver(id);
        return "service_list";
    }

    @RequestMapping(value = "queryMailName")
    @ResponseBody
    public List<MailType> queryMailName() {
        return warehouseService.queryMailName();
    }
}
